package com.hoshiicloud.user.controller.rbac;

import java.time.LocalDateTime;

import javax.validation.Valid;

import com.hoshiicloud.user.param.rbac.SysPlatformInsertParam;
import com.hoshiicloud.user.param.rbac.SysPlatformUpdateParam;
import com.hoshiicloud.user.service.rbac.ISysPlatformService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.rbac.entity.SysPlatform;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 系统组织 前端控制器
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-22
 */
@RestController
@RequestMapping("/platform")
@Api(tags = "平台基本资料")
public class SysPlatformController {
	@Autowired
	private ISysPlatformService sysPlatformService;

	@ApiOperation("新增平台")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response addPlatform(@Valid @RequestBody SysPlatformInsertParam param) {
		SysPlatform entity = new SysPlatform();
		BeanUtils.copyProperties(param, entity);
		// 调用内置工具产生ID
		entity.setId(IdWorker.getId());
		entity.setCreatedTime(LocalDateTime.now());
		// TODO 获取创建人信息
		entity.setCreatedBy("admin");
		// TODO 只接受指定参数
		boolean success = sysPlatformService.save(entity);
		if (success) {
			return Response.success("新增成功");
		} else {
			return Response.failed("新增失败");
		}
	}

	@ApiOperation("修改平台资料")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response updatePlatform(@Valid @RequestBody SysPlatformUpdateParam param) {
		SysPlatform entity = new SysPlatform();
		BeanUtils.copyProperties(param, entity);
		// TODO 获取操作人信息
		entity.setModifiedBy("admin");
		entity.setModifiedTime(LocalDateTime.now());
		boolean success = sysPlatformService.updateById(entity);
		if (success) {
			return Response.success("修改成功");
		} else {
			return Response.failed("修改失败");
		}
	}

	@ApiOperation("删除平台资料")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response deletePlatform(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		boolean success = sysPlatformService.removeById(id);
		if (success) {
			return Response.success("删除成功");
		} else {
			return Response.failed("删除失败");
		}
	}

	@ApiOperation("获取部门详情")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<SysPlatform> getPlatformDetail(@RequestParam String id) {
		SysPlatform dept = sysPlatformService.getById(id);
		if (dept != null) {
			return Response.data(dept);
		} else {
			return Response.failed("数据不存在");
		}
	}
}
